//
//  SDResolveBindView.m
//  aimigene
//
//  Created by caominglei on 2019/6/14.
//  Copyright © 2019 pony. All rights reserved.
//

#import "YQPIntegerPayView.h"

@implementation YQPIntegerPayView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initView];
    }
    return self;
}

- (void)initView
{
    [self setUpBgView];
    
    [self initCodeSubviews];
    
    [self initCodeSubviewsMasonry];
}

- (void)setUpBgView
{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.4];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)]];
    if (sdview == nil) {
        sdview = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 290, SCREEN_WIDTH, 290)];
        sdview.backgroundColor = [UIColor whiteColor];
        [self addSubview:sdview];
        
        //得到view的遮罩路径
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:sdview.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(12,12)];
        //创建 layer
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = sdview.bounds;
        //赋值
        maskLayer.path = maskPath.CGPath;
        sdview.layer.mask = maskLayer;
    }
}

- (void)showInView{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:sdview];
    [sdview setFrame:CGRectMake(0, SCREEN_HEIGHT + 290, kScreenWidth, 290)];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
        [self->sdview setFrame:CGRectMake(0, SCREEN_HEIGHT - 290,kScreenWidth, 290 )];
    } completion:nil];
}

- (void)dismissView{
    [sdview setFrame:CGRectMake(0, (SCREEN_HEIGHT - 290), SCREEN_WIDTH, 290)];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         self.alpha = 0.0;
                         
                         [self->sdview setFrame:CGRectMake(0, SCREEN_HEIGHT - 290, SCREEN_WIDTH, 290)];
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                         [self->sdview removeFromSuperview];
                         
                     }];
}

- (void)show {
    [self showInView];
}

- (void)initCodeSubviews
{
    [sdview addSubview:self.bgView];
    
    [self.bgView addSubview:self.deleteBtn];

    [self.bgView addSubview:self.titleLabel];
    
    [self.bgView addSubview:self.horiLineView];
        
    [self.bgView addSubview:self.labSubTitle];
    
    [self.bgView addSubview:self.sureBtn];
    
}

- (void)initCodeSubviewsMasonry
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(9);
        make.left.mas_equalTo(20);
        make.height.width.mas_equalTo(22);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44.f);
        make.left.top.right.mas_equalTo(self.bgView);
    }];
    
    [self.labSubTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.horiLineView.mas_bottom).offset(56);
        make.left.right.mas_equalTo(self.horiLineView);
        make.height.mas_equalTo(33);
    }];
    
    [self.horiLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(44.f);
        make.left.right.mas_equalTo(self.bgView);
        make.height.mas_equalTo(1);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.left.mas_equalTo(12);
        make.height.mas_equalTo(32);
        make.bottom.mas_equalTo(-32);
    }];
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = UIColor.whiteColor;
        _bgView.layer.cornerRadius = 8;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = kYQLS(@"确定支付");
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = kFont_Regular(17.f);
        _titleLabel.textColor = kHexString(@"#414141");
        _titleLabel.numberOfLines =0;
        [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    }
    return _titleLabel;
}

- (UILabel *)labSubTitle
{
    if (!_labSubTitle) {
        _labSubTitle = [UILabel new];
        _labSubTitle.text =[NSString stringWithFormat:@"0 %@",kYQLS(@"积分")];
        _labSubTitle.textAlignment = NSTextAlignmentCenter;
        _labSubTitle.font = kFont_Regular(24.f);
        _labSubTitle.textColor = kHexString(@"#414141");
        _labSubTitle.numberOfLines =0;
        [_labSubTitle setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    }
    return _labSubTitle;
}

- (UIView *)horiLineView
{
    if (!_horiLineView) {
        _horiLineView = [UIView new];
        _horiLineView.backgroundColor = kHexString(@"#EEEEEE");
    }
    return _horiLineView;
}

- (UIButton *)deleteBtn
{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton new];
        _deleteBtn.backgroundColor = UIColor.whiteColor;
        [_deleteBtn setBackgroundImage:IMAGE_NAMED(@"cancelPay") forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (QMUIGhostButton *)sureBtn
{
    if (!_sureBtn) {
        _sureBtn = [QMUIGhostButton new];
        _sureBtn.cornerRadius = 16;
        _sureBtn.ghostColor = kHexString(@"#F88282");
        _sureBtn.backgroundColor = kHexString(@"#F88282");
        [_sureBtn addTarget:self action:@selector(onClickPayView) forControlEvents:UIControlEventTouchUpInside];
        [_sureBtn setTitle:kYQLS(@"立即支付积分") forState:UIControlStateNormal];
        [_sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = kFont_Medium(15.f);
    }
    return _sureBtn;
}

- (void)onClickPayView
{
    if (self.sureBlock) {
        self.sureBlock();
    }
}

- (void)setModel:(YQPExchangeIntegerModel *)model
{
    _model = model;
    self.labSubTitle.text = [NSString stringWithFormat:@"%@ %@",model.totalAmount,kYQLS(@"积分")];
}
@end
